#
# @lc app=leetcode.cn id=454 lang=python3
#
# [454] 四数相加 II
#
from typing import List
# @lc code=start
class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        d = dict()
        for num1 in nums1:
            for num2 in nums2:
                d[num1+num2] = d.get(num1+num2, 0) + 1

        ans = 0
        for num1 in nums3:
            for num2 in nums4:
               if -(num1 + num2) in d:
                   ans += d[-(num1+num2)]
        return ans 
         
# @lc code=end

